<template>
  <div>
    <!-- Login -->

    <div class="center">
      <div class="img">
        <img src="../assets/avatar.jpg" alt="" />
      </div>
      <el-form
        :model="LoginForm"
        :rules="Loginrules"
        ref="ruleForm"
        label-width="80px"
      >
        <el-form-item label="用户名" prop="username" ref="FromRef">
          <el-input
            v-model="LoginForm.username"
            prefix-icon="el-icon-user-solid"
            placeholder="请输入用户名或手机号"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="LoginForm.password"
            type="password"
            prefix-icon="el-icon-key"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-button type="primary" class="login" @click="loginFn">登录</el-button>
    </div>
  </div>
</template>

<script>
import { userLoginAPI } from "../apis/user";
import { setItem, setUserId } from "../utils/localDate";
export default {
  data() {
    return {
      LoginForm: {
        username: "",
        password: "",
      },
      Loginrules: {
        username: [
          // 🔔 pattern 配置项可以指定正则验证
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            pattern: /^1\d{10}$|^\d{5,6}$/,
            min: 3,
            max: 10,
            message: "输入正确的手机号或用户名",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 15,
            message: "长度在 3 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    loginFn() {
      this.$refs.ruleForm.validate(async (val) => {
        if (!val) return this.$message.error("校验失败");
        const { data: res } = await userLoginAPI(this.LoginForm);
        console.log(res);
        if (res.message == "登录成功") {
          //   保存token和id
          setItem(res.data.token);
          setUserId(res.data.user.id);
          this.$message.success("登录成功");
          //   跳转到指定路由
          this.$router.push("/home");
          //   清空表单
          this.$refs.ruleForm.resetFields();
        } else {
          this.$message.error("登录失败");
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.img {
  width: 90px;
  height: 90px;
  background-color: red;
  border-radius: 50%;
  position: absolute;
  top: -50px;
  overflow: hidden;
  border: 10px solid #fff;
  img {
    width: 100%;
    height: 100%;
  }
}
.center {
  width: 400px;
  height: 300px;
  background-color: #eee;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  padding: 0 10px;
  display: flex;
  align-items: center;
  flex-direction: column;
  justify-content: center;
}
.el-form {
  width: 100%;
  padding: 0 10px;
}
.login {
  width: 100%;
}
</style>
